var SZModel           = require('./SZModel');
var SZProto           = require('./SZProto');
var SZLogic           = require('./SZLogic');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {

	},

	onLoad: function () {
		this.compareNodeArray = [];
		this.node.active = false;
		cc.find('Layout/AddScorex1/AddBtns', this.node).active = false;
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	gameInit: function () {
		let addScores = SZModel.getAddScores();
		let gameRule = SZModel.getGameRule();
		if (SZModel.getCurScore() >= gameRule.maxScore*gameRule.baseScore) {
			for (let i of [1, 2, 3, 4, 5, 10, 20]) {
				cc.find('Layout/AddScorex'+i, this.node).active = false;
			}
		}
		else {
			/* for (let i of [1, 2, 3, 4, 5, 10, 20]) {
				cc.find('Layout/AddScorex'+i, this.node).active = (addScores.indexOf(i) != -1);
			} */
			cc.find('Layout/AddScorex1', this.node).active = true;
			/* cc.find('Layout/AddScorex2', this.node).active = true; */
			/* cc.find('Layout/AddScorex3', this.node).active = true; */
		}
		if (SZModel.getCurChairID() == SZModel.getMyChairID() && SZModel.getGameStatus() == SZProto.gameStatus.POUR_SCORE) {
			this.node.active = true;
			let men = SZModel.getGameRule().gameFrameType;
			let userStatus = SZModel.getUserStatusByChairID(SZModel.getMyChairID());
			if ((userStatus&SZProto.userStatus.LOOK) > 0) {
				cc.find('Layout/Cuopai', this.node).active = false;
			}
			else {
				cc.find('Layout/Cuopai', this.node).active = SZModel.getGameRule().cuopai && (men < SZModel.getCurRound());
			}
			cc.find('Layout/Bipai', this.node).active = (men < SZModel.getCurRound());
		}
		else {
			this.node.active = false;
			cc.find('Layout/AddScorex1/AddBtns', this.node).active = false;
		}
	},

	messageCallbackHandler: function(router, msg) {
		if (!SZModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === SZProto.GAME_LOOK_PUSH) {
				if (msg.data.chairID == SZModel.getMyChairID()) {
					this.gameInit();
				}
			}
			else if(msg.type === SZProto.GAME_TURN_PUSH) {
				this.gameInit();
			}
			else if(msg.type === SZProto.GAME_STATUS_PUSH) {
				this.gameInit();
			}
			else if(msg.type === SZProto.GAME_COMPARE_PUSH) {
				let audio = 'Game/Sanzhang/Audio/woman/cmp';
				let user = SZModel.getUserByChairID(msg.data.fromChairID);
				if (user && user.userInfo.sex == 0) {
					audio = audio.replace(/woman/, "man");
				}
				Global.AudioManager.playSound(audio);
			}
			else if(msg.type === SZProto.GAME_POUR_SCORE_PUSH) {
				let audio = "";
				if (msg.data.type == 1) {
					audio = 'Game/Sanzhang/Audio/woman/call';
				}
				else if (msg.data.type == 2) {
					audio = 'Game/Sanzhang/Audio/woman/raise';
				}
				let user = SZModel.getUserByChairID(msg.data.chairID);
				if (user && user.userInfo.sex == 0) {
					audio = audio.replace(/woman/, "man");
				}
				Global.AudioManager.playSound(audio);
			}
			else if(msg.type === SZProto.GAME_ABANDON_PUSH) {
				let audio = 'Game/Sanzhang/Audio/woman/fold';
				let user = SZModel.getUserByChairID(msg.data.chairID);
				if (user && user.userInfo.sex == 0) {
					audio = audio.replace(/woman/, "man");
				}
				Global.AudioManager.playSound(audio);
				if (msg.data.chairID == SZModel.getMyChairID()) {
					while (this.compareNodeArray.length > 0) {
						let node = this.compareNodeArray.pop();
						node.destroy();
					}
					cc.find('bg', this.node).active = false;
				}
			}
		}
	},

	onAbandonClick: function () {
		Global.NetworkManager.notify(GameMessageRouter, SZProto.gameAbandonNotify());
		this.node.active = false;
		cc.find('Layout/AddScorex1/AddBtns', this.node).active = false;
	},

	onMingPaiClick: function () {
		Global.NetworkManager.notify(GameMessageRouter, SZProto.gameMingPaiNotify());
	},

	onCompareClick: function () {
		let jifen = this.node.parent.getChildByName("Head1").getChildByName("id").getComponent(cc.Label).string;
			if(Number(jifen)-SZModel.getCurScore() <=0 ){
				Global.DialogManager.addTipDialog('积分不足，请及时充值积分');
				return;
			}
		let szMainLogic = this.node.parent.getComponent('SZMainDialog');
		let canCompareArray = SZModel.getCanCompareChairIDs();
		let bgNode = cc.find('bg', this.node);
		bgNode.active = true;
		let compareNode = cc.find('Compare', this.node);
		let selfPos = this.node.getPosition();
		for (let chairID of canCompareArray) {
			if (chairID != SZModel.getMyChairID()) {
				let headNodePos = szMainLogic.getHeadNodeByChairID(chairID).getPosition();
				let node = cc.instantiate(compareNode);
				node.parent = this.node;
				node.active = true;
				node.setPosition(cc.v2(headNodePos.x-selfPos.x, headNodePos.y-selfPos.y));
				this.compareNodeArray.push(node);
				node.on(cc.Node.EventType.TOUCH_START, this.compare.bind(this, chairID));
			}
		} 
	},

	compare: function (chairID) {
		while (this.compareNodeArray.length > 0) {
			let node = this.compareNodeArray.pop();
			node.destroy();
		}
		cc.find('bg', this.node).active = false;
		if (SZModel.isUnionCreate()) {
			let myScore = SZModel.getCurScoreByChairID(SZModel.getMyChairID());
			let lookCard = SZModel.getLookCardByChairID(SZModel.getMyChairID());
			let curScore = SZModel.getCurScore() * (lookCard? 2:1);
			/* if (myScore-curScore < 0) {
				Global.DialogManager.addTipDialog('比牌金币不足,只能弃牌');
				return;
			} */
		}
		Global.NetworkManager.notify(GameMessageRouter, SZProto.gameCompareNotify(chairID));
		this.node.active = false;
		cc.find('Layout/AddScorex1/AddBtns', this.node).active = false;
	},

	cancelCompare: function () {
		while (this.compareNodeArray.length > 0) {
			let node = this.compareNodeArray.pop();
			node.destroy();
		}
		cc.find('bg', this.node).active = false;
	},

	onCuopaiClick: function () {
		Global.AudioManager.playCommonSoundClickButton();
		Global.NetworkManager.notify(GameMessageRouter, SZProto.gameLookCardsNotify(true));
	},

	onGenzhuClick: function () {
		
		if (SZModel.isUnionCreate()) {
			let myScore = SZModel.getCurScoreByChairID(SZModel.getMyChairID());
			let lookCard = SZModel.getLookCardByChairID(SZModel.getMyChairID());
			let curScore = SZModel.getCurScore() * (lookCard? 2:1);
			if (myScore-curScore < 0) {
				Global.DialogManager.addTipDialog('下注不能超过自己拥有的金币');
				return;
			}
		}else{
			let jifen = this.node.parent.getChildByName("Head1").getChildByName("id").getComponent(cc.Label).string;
			if(Number(jifen)-SZModel.getCurScore() <=0 ){
				Global.DialogManager.addTipDialog('积分不足，请及时充值积分');
				return;
			}
		}
		Global.NetworkManager.notify(GameMessageRouter, SZProto.gamePourScoreNotify(SZModel.getCurScore(), 1));
		this.node.active = false;
		cc.find('Layout/AddScorex1/AddBtns', this.node).active = false;
	},

	onAddScoreClick: function (event, param) {
		let index = parseInt(param);
		let score = SZModel.getCurScore()+index*SZModel.getGameRule().baseScore;
		let lookCard = SZModel.getLookCardByChairID(SZModel.getMyChairID());
		/* if (lookCard) { score *= 2; } */
		if (SZModel.isUnionCreate()) {
			let myScore = SZModel.getCurScoreByChairID(SZModel.getMyChairID());
			if (myScore-((lookCard)? score*2:score) < 0) {
				Global.DialogManager.addTipDialog('下注不能超过自己拥有的金币');
				return;
			}
		}else{
			let jifen = this.node.parent.getChildByName("Head1").getChildByName("id").getComponent(cc.Label).string;
			if(Number(jifen)-score <=0 ){
				Global.DialogManager.addTipDialog('积分不足，请及时充值积分');
				return;
			}
		}
		Global.NetworkManager.notify(GameMessageRouter, SZProto.gamePourScoreNotify(score, 2));
		this.node.active = false;
		cc.find('Layout/AddScorex1/AddBtns', this.node).active = false;
	},

	onShowAddBtnsClick: function () {
		let addBtns = cc.find('Layout/AddScorex1/AddBtns', this.node);
		addBtns.active = !addBtns.active;
		for (let i = 1; i <= 4; ++i) {
			let btnLabel = cc.find('Add'+i+'/Background/Label', addBtns);
			if (btnLabel) {
				btnLabel.getComponent(cc.Label).string = "+"+(i*SZModel.getGameRule().baseScore);
			}
		}
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},
});
